#include <bits/stdc++.h>
using namespace std;

// #define ONLINE_JUDGE

#ifndef ONLINE_JUDGE
#define dbg(x...)                             \
	{                                         \
		cerr << "\033[32;1m" << #x << " -> "; \
		err(x);                               \
	}
void err()
{
	cerr << "\033[39;0m" << endl;
}
template <typename T, typename... A>
void err(T a, A... x)
{
	cerr << a << ' ';
	err(x...);
}
#else
#define dbg(...)
#endif

typedef long long LL;
#define int long long

const int N = 2e5 + 50;

int n, p[N], a[N], pos[N];

LL t[N];
LL ans = 1e18;

void init()
{
	LL curcost = 0;
	LL tans = 1e18;
	for (int i = 1; i <= n - 1; ++i)
	{
		curcost += a[i];
		t[i] = curcost;
		tans = min(tans, t[i]);
	}
	ans = min(ans, tans);
}

struct segT
{
	LL dat[N << 2];
	int nn;
	LL lazy[N << 2];

	inline void pu(int rt)
	{
		dat[rt] = min(dat[rt << 1], dat[rt << 1 | 1]);
	}

	void init(LL a[], int n)
	{
		nn = 1;
		while (nn < n)
			nn <<= 1;
		memset(lazy, 0, sizeof(LL) * (2 * nn));
		for (int i = 1; i <= n; ++i)
			dat[i + nn - 1] = a[i];
		for (int i = nn + n; i <= 2 * nn; ++i)
			dat[i] = LLONG_MAX;
		for (int i = nn - 1; i >= 1; --i)
			pu(i);
	}

	inline void pd(int rt)
	{
		if (lazy[rt] != 0)
		{
			int ls = rt << 1, rs = rt << 1 | 1;
			dat[ls] += lazy[rt];
			lazy[ls] += lazy[rt];
			dat[rs] += lazy[rt];
			lazy[rs] += lazy[rt];
			lazy[rt] = 0;
		}
	}

	int L, R;
	void upt(int l, int r, int rt, int v)
	{
		if (L <= l && r <= R)
		{
			dat[rt] += v;
			lazy[rt] += v;
			return;
		}
		int m = (l + r) >> 1;
		pd(rt);
		if (L <= m)
			upt(l, m, rt << 1, v);
		if (m + 1 <= R)
			upt(m + 1, r, rt << 1 | 1, v);
		pu(rt);
	}

	LL q(int l, int r, int rt)
	{
		if (L <= l && r <= R)
			return dat[rt];
		int m = (l + r) >> 1;
		pd(rt);
		LL res = LLONG_MAX;
		if (L <= m)
			res = min(res, q(l, m, rt << 1));
		if (m + 1 <= R)
			res = min(res, q(m + 1, r, rt << 1 | 1));
		pu(rt);
		return res;
	}
	inline LL q(int l, int r)
	{
		L = l;
		R = r;
		return q(1, nn, 1);
	}
	inline void u(int l, int r, int v)
	{
		if (l > r) return;
		L = l;
		R = r;
		upt(1, nn, 1, v);
	}
} seg;

signed main()
{
	scanf("%lld", &n);
	for (int i = 1; i <= n; ++i)
	{
		scanf("%lld", &p[i]);
		pos[p[i]] = i;
	}
	for (int i = 1; i <= n; ++i)
		scanf("%lld", &a[i]);
	init();
	seg.init(t, n - 1);

	for (int val = 1; val <= n; ++val)
	{
		// for (int i=1; i<=n-1; ++i)	dbg(i, seg.q(i, i));
		int p = pos[val];
		dbg(p, val, ans, a[p]);
		if (p > 1)
			seg.u(1, p - 1, a[p]);
		if (p < n)
			seg.u(p, n - 1, -a[p]);
		LL tans = seg.q(1, n - 1);
		ans = min(ans, tans);
	}
	printf("%lld\n", ans);

	return 0;
}